package com.example.testhttp;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


/*
 * 按钮下载图片到UI上
 */
public class MainActivity extends Activity {

	

	Button  my_testbt = null;
	ImageView my_imageview = null;
	Dialog my_dDialog = null;
	
	
	//String strImageURL = "https://www.baidu.com/img/bd_logo1.png";
	String strImageURL = "http://jesonweb.qiniudn.com/wp-content/uploads/2012/10/201021293802-300x209.jpg";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		my_testbt = (Button)findViewById(R.id.button1);
		my_imageview = (ImageView)findViewById(R.id.imageView1);
		
		my_testbt.setText("下载图片");
		my_testbt.setOnClickListener(new ClickDown());
		
		my_dDialog = new ProgressDialog(this);
		my_dDialog.setTitle("正在下载，请稍候...");
		my_dDialog.setCancelable(false);

	}

	
	public class ClickDown implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			new HttpAsyncTask().execute(strImageURL);

		}

	}
	
	
	
	public class HttpAsyncTask extends AsyncTask<String, Void, Bitmap> 
	{
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			my_dDialog.show();
		}
		

		@Override
		protected Bitmap doInBackground(String... params) {
			// TODO Auto-generated method stub
			Bitmap m_Bitmap = null;
			HttpClient mHttpClient = new DefaultHttpClient();
			HttpGet mHttpGet = new HttpGet(params[0]);
			try 
			{
				HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
				if (mHttpResponse.getStatusLine().getStatusCode() == 200)
				{
					HttpEntity mHttpEntity = mHttpResponse.getEntity();
					byte[] data = EntityUtils.toByteArray(mHttpEntity);
					m_Bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
					
				}
			}
			catch (Exception e) 
			{
				// TODO: handle exception
			}
			return m_Bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			my_imageview.setImageBitmap(result);
			my_dDialog.dismiss();
		}


	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
